package net.cl.utils;

import java.io.IOException;
import java.util.Hashtable;
import java.util.StringTokenizer;

import net.cl.tools.HttpProxy;

public class HttpUtil {

	public static String getResponseString(String url) {
		HttpProxy p = new HttpProxy(url);
		try {
			return p.get();
		} catch (IOException e) {
			return null;
		}
	}

	public static String getResponseString(String url, String encoding) {
		HttpProxy p = new HttpProxy(url);
		try {
			p.encoding(encoding);
			return p.get();
		} catch (IOException e) {
			return null;
		}
	}

	public static String finalRedirect(String url) {
		HttpProxy p = new HttpProxy(url);
		try {
			return p.finalRedirect();
		} catch (IOException e) {
			return null;
		}
	}

	public static long download(String url, String targetPath) {
		HttpProxy p = new HttpProxy(url);
		try {
			return p.download(targetPath);
		} catch (IOException e) {
			return 0L;
		}
	}

	public static String getParameter(String url, String param) {
		if (!url.contains("?"))
			url = "?" + url;
		String[] tmp = url.split("\\?");
		Hashtable<String, String[]> ht = parseQueryString(tmp[1]);
		String[] tmp2 = ht.get(param);
		if (tmp2 == null)
			return null;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < tmp2.length; i++) {
			if (i > 0)
				sb.append(";");
			sb.append(tmp2[i]);
		}
		return sb.toString();
	}

	private static Hashtable<String, String[]> parseQueryString(String s) {
		String valArray[] = null;
		Hashtable<String, String[]> ht = new Hashtable<String, String[]>();
		StringBuffer sb = new StringBuffer();
		StringTokenizer st = new StringTokenizer(s, "&");
		while (st.hasMoreTokens()) {
			String pair = (String) st.nextToken();
			int pos = pair.indexOf('=');
			if (pos == -1)
				throw new IllegalArgumentException();
			String key = parseName(pair.substring(0, pos), sb);
			String val = parseName(pair.substring(pos + 1, pair.length()), sb);
			if (ht.containsKey(key)) {
				String oldVals[] = ht.get(key);
				valArray = new String[oldVals.length + 1];
				for (int i = 0; i < oldVals.length; i++)
					valArray[i] = oldVals[i];
				valArray[oldVals.length] = val;
			} else {
				valArray = new String[1];
				valArray[0] = val;
			}
			ht.put(key, valArray);
		}
		return ht;
	}

	private static String parseName(String s, StringBuffer sb) {
		sb.setLength(0);
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			switch (c) {
			case '+':
				sb.append(' ');
				break;
			case '%':
				try {
					sb.append((char) Integer.parseInt(s.substring(i + 1, i + 3), 16));
					i += 2;
				} catch (NumberFormatException e) {
					throw new IllegalArgumentException();
				} catch (StringIndexOutOfBoundsException e) {
					String rest = s.substring(i);
					sb.append(rest);
					if (rest.length() == 2)
						i++;
				}
				break;
			default:
				sb.append(c);
				break;
			}
		}
		return sb.toString();
	}

}
